<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>新增轮播</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
</head>
<body>
<div style="width:80%;margin:15px auto;overflow:auto;" id="aaa">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">轮播标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" id="title" required  lay-verify="required" placeholder="请输入轮播标题" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">轮播类型</label>
            <div class="layui-input-block">
                <select lay-verify="required" lay-search id="type" name="type">
                    <option id="ccc" disabled selected></option>
                    <option value="1">地址</option>
                    <option value="0">富文本</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">通知内容</label>
            <div class="layui-input-block">
                <textarea id="content" name="content" class="layui-textarea" style="display: none;" lay-verify="content" ></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">跳转地址</label>
            <div class="layui-input-block">
                <input type="text" name="url" id="url"  placeholder="请输入跳转地址" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <select lay-verify="required" lay-search id="status" name="status">
                    <option id="bbb" disabled selected></option>
                    <option value="1">启用</option>
                    <option value="0">禁用</option>
                </select>
            </div>
        </div>

        <div class="layui-upload">
            <button type="button" class="layui-btn" id="test" style="display: inline;float: left;">上传图片</button>
            <div class="layui-upload-list" style="display: inline;margin-left: 30px;">
                <img class="layui-upload-img" name="image" id="image" style="width: 200px;height: 150px">
                <p id="demoText"></p>
            </div>
            <input type="hidden" class="input-text" id="logo" name="logo">
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <div class="layui-input-block">
                <button class="layui-btn" id="addShouye_lunbo" lay-submit lay-filter="addShouye_lunbo">立即添加</button>
                <button type="reset" id="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/js/layui/layui.js"></script>
<script src="/js/layui/layui.all.js"></script>
<script src="/admin/js/token.js"></script>

<script>

    layui.use(['form' , 'jquery' , 'upload', 'layedit'], function(){
        var form = layui.form;
        var $ = layui.jquery;
        var upload = layui.upload;
        var layedit = layui.layedit;
        layedit.set({
            uploadImage: {
                url: '/uploadImage', //接口url
                type: 'post' //默认post
            }
        });
        var index = layedit.build('content',{
            tool: ['strong','italic','underline','del' ,'|','left','center','right','link','unlink','image','face']
        });

        //提交时把值同步到文本域中
        form.verify({
            //content富文本域中的lay-verify值
            content: function(value) {
                return layedit.sync(index);
            }
        });

        //添加操作
        form.on('submit(addShouye_lunbo)',function(data){
            var status = $("#status").find("option:selected").val();
            data.field.status = status;
            var type = $("#type").find("option:selected").val();
            data.field.type = type;
            var content = layedit.getContent(index);
            var image = $("#logo").val();
            data.field.image = image;
            var title = $("#title").val();
            if(title==null || title==""){
                layer.msg('请填写标题！');
            }else if (type == null || type == "") {
                layer.msg('请选择轮播类型！');
            }else if(content==null || content==""){
                layer.msg('请填写内容！');
            }else if (status == null || status == "") {
                layer.msg('请选择状态！');
            }else if (image == null || image == "") {
                layer.msg('请上传图片！');
            } else {
                if (type == 0 && (content==null || content=="")){
                    layer.msg('请填写内容！');
                    return false;
                }
                if (type == 1 && $("#url").val()==""){
                    layer.msg('请填写地址！');
                    return false;
                }
                $.ajax({
                    url: "/appshouye/saveLunbo"
                    , type: "POST"
                    , contentType: 'application/json'
                    , data: JSON.stringify(data.field)
                    , success: function (data) {
                        if (data.code == 500) {
                            layer.msg('添加失败！')
                        } else if(data.code==200) {
                            layer.close(layer.index);
                            layer.msg('添加成功！');
                            setTimeout(function () {
                                window.location.reload();
                            }, 3000);
                        }else{
                            layer.msg('拒绝访问');
                        }
                    }
                    ,error:function () {
                        console.log("ajax error");
                    }
                })
            }
            return false;
        });

        //logo上传
        var uploadInst = upload.render({
            elem: '#test'
            , url: '/upload' //改成您自己的上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#image').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //判断上传是否成功
                if (res.code == 0) {
                    $("#logo").val(res.data);
                    return layer.msg('上传成功！');
                }else{
                    return layer.msg('上传失败！');
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
    });
</script>
</body>
</html>